<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ShopersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $shops = DB::table('shopers')->get();
        // dd($shops);
       return view('admin.shop.shopList',['shops'=>$shops]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($_POST);
        $res = DB::table('shopers')->insert(
            ['phone'=>$_POST['phone'],
            'shopername'=>$_POST['shopername'],
            'password'=>$_POST['password'],
            ]
            );
        if(!empty($res)){
            $shopers = DB::table('shopers')->where('phone','=',$_POST['phone'])->first();
            // dd($shopers);
            DB::table('shops')->insert(['spid'=>$shopers->id]);
            return redirect('admin/shop');
        }
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $shops = DB::table('shopers')->where('id', $id)->first();
        // dd($shops);
        if(!empty($shops)){
            return view('admin.shop.edit', ['shops'=>$shops]);
        }
        return back();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($_POST);
         $res = DB::table('shopers')
            ->where('id', $id)
            ->update(['shopername' =>  $_POST['shopername'],
            'phone' =>  $_POST['phone'],
            'password' =>  $_POST['password']]
            );
            // dd($res);
            if(!empty($res)){
                return redirect('admin/shop');
            }

            return back();

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('shopers')->where('id',$id)->delete();
        return back();
    }
}
